<?php
/**
 * Created by PhpStorm.
 * User: 图图
 * Date: 2021-6-8
 * Time: 16:29
 */

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\Factor;
use app\common\model\Fenorder;
use app\common\model\Cart as CartModel;
use think\Db;
use think\db\Where;
use function Couchbase\defaultDecoder;

/**
 * 前台首页控制器
 * @package app\index\controller
 */
class Cart extends Home
{
    public $noNeedLogin = [];



    /**
     * 添加到购物车
     */
    public function add(){
        if($this->request->isPost()){
            $getData= $this->request->post();
            $validate = new \app\api\validate\Cart();
            if (!$validate->scene('add')->check($getData)) {
                $this->result($validate->getError(), '', 204);
            }
            $getData['create_time'] = time();
            $getData['user_id'] = $this->userInfo['id'];
            $status = CartModel::checkCart($getData);
            if($status == true){
                $insert = CartModel::where(['goods_id'=>$getData['goods_id'],'user_id'=>$getData['user_id'],'sku_id'=>$getData['sku_id']])->setInc('num',$getData['num']);
            }else{
                $insert = CartModel::create($getData);
            }
            if($insert){
                $this->result('加入购物车成功','',200);
            }
            $this->result('加入购物车失败','',204);
        }
        $this->result('无效的请求','',204);
    }

    /**
     * 购物车列表
     */
    public function lists(){
        $userInfo = $this->userInfo;
        $pageData = $this->request->get('limit');
        if(empty($pageData) || !isset($pageData)){
            $limit = 10;
        }else{
            $limit = $pageData;
        }
        $cartList = Db::name('qt_shopcart')
            ->field('id,goods_id,num,create_time,sku_id,sku_val')
            ->where(['user_id'=>$userInfo['id']])->paginate($limit)->each(function ($item){
            $goods = Db::name('qt_goods')->where('goods_id',$item['goods_id'])->find();
            $goodsSku = Db::name('qt_goods_sku')->where('id',$item['sku_id'])->find();
            $item['goods_name'] = $goods['goods_name'];
            $item['sku_price'] = $goodsSku['sku_price'];
            $item['goods_price'] = $goodsSku['sku_price'] * $item['num'];
            $item['goods_img'] = get_img_path($goodsSku['sku_img']);
            return $item;
        });
        $this->result('查询成功',$cartList,200);
    }


    /**
     * 编辑购物车
     * @throws \think\Exception
     */
    public function edit(){
        if($this->request->isPost()){
            $getData= $this->request->post();
            $validate = new \app\api\validate\Cart();
            if (!$validate->scene('update')->check($getData)) {
                $this->result($validate->getError(), '', 204);
            }
            $getData['user_id'] = $this->userInfo['id'];
            $update = CartModel::where(['id'=>$getData['id'],'user_id'=>$getData['user_id']])->update($getData);
            if($update !== false){
                $this->result('修改购物车成功','',200);
            }
            $this->result('修改购物车失败','',204);
        }
        $this->result('无效的请求','',204);
    }

    /**
     * 删除购物车
     * @throws \Exception
     */
    public function del(){
        if($this->request->isPost()){
            $userInfo = $this->userInfo;
            $getData= $this->request->post();
            if(empty($getData['id']) || !isset($getData['id'])){
                $this->result('无效的参数','',204);
            }
            $idRes = explode(',',$getData['id']);
            if(is_array($idRes)){
                foreach ($idRes as $key=>$value){
                    $update = CartModel::where(['id'=>$value,'user_id'=>$userInfo['id']])->delete();
                }
            }else{
                $this->result('无效的参数','',204);
            }
            if($update){
                $this->result('删除购物车成功','',200);
            }
            $this->result('删除购物车失败','',204);
        }
        $this->result('无效的参数','',204);
    }

}
